import * as _ from 'lodash';
import { resolveReflectiveProviders } from '@angular/core/src/di/reflective_provider';

export class TenantsService {
    datas: Array<any> = [
        {
            name: '租户001',
            network: '80%(80000/100000)',
            disk: '50%(50G/100G)',
            cpu: '30%(30/100)',
            machine: '30',
            comment: '物理机服务器'
        },
        {
            name: '租户002',
            network: '90%(90000/100000)',
            disk: '30%(30G/100G)',
            cpu: '80%(80/100)',
            machine: '80',
            comment: '物理机服务器'
        },
        {
            name: '租户003',
            network: '20%(20000/100000)',
            disk: '10%(10G/100G)',
            cpu: '100%(100/100)',
            machine: '100',
            comment: '物理机服务器'
        }
    ];
    servers: Array<any> = [
        {
            standard: 'X86',
            brand: 'HP ProLiant DL580 G9',
            provider: '北京嘉城泰和科技有限公司',
            cpu: 'Xeon E7-4809 v3 2GHz',
            memory: '32GB DDR4'
        },
        {
            standard: 'X86',
            brand: 'HP ProLiant DL388 Gen9',
            provider: '北京网络未来科技发展有限公司',
            cpu: 'Xeon E5-2630 V4 2.2GHz',
            memory: '32GB DDR4'
        },
        {
            standard: 'X86',
            brand: '浪潮英信NF5270M4',
            provider: '北京英信未来科技有限公司',
            cpu: 'Xeon E5-2609 v3 1.9GHz',
            memory: '8GB DDR4'
        },
        {
            standard: 'X86',
            brand: 'HP ProLiant DL580 G9',
            provider: '北京嘉城泰和科技有限公司',
            cpu: 'Xeon E7-4809 v3 2GHz',
            memory: '32GB DDR4'
        },
        {
            standard: 'X86',
            brand: '联想System x3850 X6',
            provider: '北京鑫创科技有限公司',
            cpu: 'Xeon E7-4809 v2 1.9GHz',
            memory: '32GB DDR3'
        },
        {
            standard: 'X86',
            brand: '戴尔PowerEdge R730',
            provider: '北京永昌高泰科技发展有限公司',
            cpu: 'Xeon E5-2640 v4 2.4GHz',
            memory: '64GB RDIMM'
        },
        {
            standard: 'POWER',
            brand: 'IBM Power System S812L',
            provider: '北京普天时代信息技术有限公司',
            cpu: 'POWER8 3.42GHz',
            memory: '64GB DDR3'
        },
        {
            standard: 'SPARC',
            brand: 'Sun SPARC Enterprise T5240',
            provider: '北京华人方创科技有限公司',
            cpu: 'UltraSPARC T2 1.4GHz',
            memory: '32GB FB-DIMM'
        },
        {
            standard: 'X86',
            brand: 'HP ProLiant DL580 G9',
            provider: '北京嘉城泰和科技有限公司',
            cpu: 'Xeon E7-4809 v3 2GHz',
            memory: '32GB DDR4'
        }
    ]



    constructor() {

    }

    queryMachine(condition) {
        let list = _.cloneDeep(this.datas);
        let result = [];
        let findResult = [];
        if (!condition) {
            return list;
        }
        let search = condition.search;
        if (search != null) {
            for (let l of list) {
                if (l.name.indexOf(search) >= 0) {
                    result.push(l);
                }
            }
        } else {
            result = list;
        }

        if (condition.sort) {
            result = _.orderBy(result, [
                condition.sort.field
            ], [condition.sort.sort]);
        }

        let pageSize = condition.pageSize;
        if (pageSize == null) {
            pageSize = 10;
        }

        let pageNo = condition.pageNo;
        if (pageNo == null) {
            pageNo = 1;
        }

        for (let i = 0; i < result.length; i++) {
            if (i >= (pageNo - 1) * pageSize) {
                findResult.push(result[i]);
            }
            if (findResult.length >= pageSize) {
                break;
            }
        }

        return {
            data: findResult,
            totalCount: result.length
        };
    }

    getDetail(name: string): any {
        let index = Math.ceil(Math.random() * this.servers.length);
        let result = this.servers[index];
        if (result == null) {
            return this.servers[0];
        }
        return result;
    }
}